package com.blm.controller.merchant;

import com.blm.common.Result;
import com.blm.dto.OrderStatusUpdateDTO;
import com.blm.entity.Order;
import com.blm.entity.User;
import com.blm.service.OrderService;
import com.blm.service.StoreService;
import com.blm.service.UserService;
import com.blm.util.SecurityUtil;
import com.blm.vo.OrderDetailVO;
import com.blm.vo.OrderVO;
import com.blm.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "商家API-店铺订单管理")
@RestController
@RequestMapping("/api/merchant/orders/{storeId}")
@SecurityRequirement(name = "bearerAuth")
public class MerchantOrdersController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityUtil securityUtil;

    @Operation(summary = "获取订单列表",description = "获取订单列表")
    @ApiResponse(responseCode = "200",description = "成功",
            content = @Content(mediaType = "application/json",
            schema = @Schema(implementation = OrderVO.class)))
    @GetMapping
    public Result<PageVO<OrderVO>> getOrders(
            @Parameter(description = "店铺id",required = true) @PathVariable Long storeId,
            @Parameter(description = "订单状态") @RequestParam(required = false) Order.OrderStatus status,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size
    ){
        verifyStoreOwnership(storeId);
        PageVO<OrderVO> orderVOPage = orderService.listStoreOrders(storeId,status,page,size);
        return Result.success(orderVOPage);
    }

    @Operation(summary = "店铺根据订单id和店铺id获取订单详情",description = "店铺根据订单id和店铺id获取订单详情")
    @ApiResponse(responseCode = "200",description = "成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderDetailVO.class)))
    @GetMapping("/orderDetail")
    public Result<OrderDetailVO> getDetailOrders(
            @Parameter(description = "店铺id",required = true) @PathVariable Long storeId,
            @Parameter(description = "订单id") @RequestParam Long id
    ){
        verifyStoreOwnership(storeId);
        OrderDetailVO orderDetailVO = orderService.getOrderDetailwithStore(id,storeId);
        return Result.success(orderDetailVO);

    }

    @Operation(summary = "店铺更新订单状态",description = "店铺根据订单id和店铺id更新订单状态")
    @ApiResponse(responseCode = "200",description = "成功")
    @PutMapping("/{orderId}/status")
    public Result<Void> updateOrderStatus(
            @Parameter(description = "店铺id",required = true) @PathVariable Long storeId,
            @Parameter(description = "订单id",required = true) @PathVariable Long orderId,
            @RequestBody(description = "新的状态信息", required = true, content = @Content(schema = @Schema(implementation = OrderStatusUpdateDTO.class))) @org.springframework.web.bind.annotation.RequestBody OrderStatusUpdateDTO dto
    ){
        verifyStoreOwnership(storeId);
        User user=userService.findUserById(securityUtil.getCurrentUserId());
        orderService.updateOrderStatusByStore(orderId,storeId,dto,user);
        return Result.success(null);
    }
    /**
     * 验证当前用户是否是店铺的所有者
     *
     * @param storeId 店铺ID
     */
    private void verifyStoreOwnership(Long storeId) {
        Long userId = securityUtil.getCurrentUserId();
        storeService.verifyStoreOwner(storeId, userId);
    }
}
